package com.ld.security.server.admin.modules.ge.service.impl;

import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.server.admin.modules.ge.entity.GeColumnEntity;
import com.ld.security.server.admin.modules.ge.entity.GeTableEntity;
import com.ld.security.server.admin.modules.ge.mapping.IGeColumnMapping;
import com.ld.security.server.admin.modules.ge.mapping.IGeTableMapping;
import com.ld.security.server.admin.modules.ge.mapping.ProxyMapping;
import com.ld.security.server.admin.modules.ge.po.TableSaveDetailsPo;
import com.ld.security.server.admin.modules.ge.service.IGeTableService;
import com.ld.security.server.admin.modules.ge.vo.TableDetailsVo;
import com.ld.security.server.admin.modules.ge.vo.TableTreeVo;
import com.ld.security.common.core.base.SuperPo;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.common.mybatis.base.vo.NodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
class GeTableServiceImpl extends ISuperServiceImpl<GeTableEntity> implements
        IGeTableService {


    @Autowired
    IGeTableMapping iGeTableMapping;

    @Autowired
    IGeColumnMapping iGeColumnMapping;

    @Autowired
    ProxyMapping proxyMapping;


    @Override
    public int removeByIds(Collection<String> ids) {
        for (String id : ids) {
            TableDetailsVo vc =   this.getTableDetails(id);
            JdbcUtil.proxyRemove(vc);
        }
        return ids.size();
    }

    @Override
    public List<NodeVo> getTableTree(SuperPo po) {

        List<GeTableEntity> tables =  this.all(po);

        TableTreeVo vo = new TableTreeVo();
        vo.setTables(tables);

        this.proxyQuery(vo);

        Map<String,List<GeColumnEntity>> tableMap =   vo.getColumns().stream().collect(Collectors.groupingBy(v -> v.getTableId()));

       return vo.getTables().stream().map(val -> {
            NodeVo parent =  new NodeVo();
            parent.setId(val.getTableName());
            parent.setLabel(val.getLabel());
          //  parent.setData(val);
            List<GeColumnEntity> vs =  tableMap.get(val.getId());
           List<NodeVo> child = null;
           if(vs != null){
               child = vs.stream().map(val2 -> {
                   NodeVo children =  new NodeVo();
                   children.setId(val.getTableName()+"." +val2.getColumnName());
                   children.setLabel(val2.getLabel());
                   // children.setData(val2);
                   return children;
               }).collect(Collectors.toList());

           }
           parent.setChildren(child);
            return parent;
        }).collect(Collectors.toList());


    }

    @Override
    public TableDetailsVo getTableDetails(String id) {
        TableDetailsVo vo = new TableDetailsVo();
        vo.setId(id);
        this.proxyQuery(vo);
        return vo;
    }

    @Override
    public void tableSaveDetailsPo(@RequestBody TableSaveDetailsPo po){
        this.proxySave(po);
    }

}
